package com.edais.service.imp;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.edais.actions.PerMatchDetailAction;
import com.edais.dao.FinanceDao;
import com.edais.dao.FinancePlanOpfDao;
import com.edais.domain.Finance;
import com.edais.service.EPlanAssignService;
import com.edais.util.CommonUtil;
import com.edais.util.Consts;
import com.edais.util.JsonUtil;
import com.edais.util.Message;

@Service

@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public class EPlanAssignServiceImp implements EPlanAssignService {
	private static final Log logger = LogFactory.getLog(PerMatchDetailAction.class);
	
	@Autowired
	private FinanceDao financeDao;
	
	@Autowired
	private FinancePlanOpfDao financePlanOpfDao;

	/**
	 * 已转让订单列表
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	public Map<String, Object> getAssignCompOrderList(Map<String, Object> map) throws Exception{
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		String transCode = "E330019";
		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取转让中订单列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}
		return rtnMap;
	}
	
	/**
	 * 转让中订单列表
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	public Map<String, Object> getAssignInOrderList(Map<String, Object> map) throws Exception{
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		String transCode = "E330018";
		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取转让中订单列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}
		return rtnMap;
	}
	
	/**
	 * 获取e计划系列下可转让订单列表
	 * @param
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class,readOnly=false)
	public Map<String, Object> getOrderList(Map<String, Object> map) throws Exception{
		Map<String, Object> rtnMap = new HashMap<String, Object>();
		String transCode = "E330006";
		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取e计划系列下可转让订单列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}
		return rtnMap;
	}

	/**
	 * 获取e计划系列下可转让债权列表
	 * @param 
	 */
	@Override
	public Map<String, Object> getAssignList(Map<String, Object> map) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();

		String transCode = "E330007";

		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> tmpMap = list.get(i);
					tmpMap.put("creditState", "回款中");     // 设置交易状态
					
					list.set(i, tmpMap);
				}
				
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
				
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			
			rtnMap.put("preMatchAmount", jsonObject.getString("preMatchAmount"));
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			rtnMap.put("creditTotalValue", jsonObject.getString("creditTotalValue"));
			
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取e计划系列下可转让债权列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}

		return rtnMap;
	}

	/**
	 * 获取e计划系列下转让中债权列表
	 * @param 
	 */
	@Override
	public Map<String, Object> getInAssignList(Map<String, Object> map) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();

		String transCode = "E330008";

		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> tmpMap = list.get(i);
					tmpMap.put("creditState", "转让中");     // 设置交易状态
					
					list.set(i, tmpMap);
				}
				
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
				
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			
			rtnMap.put("totalCreditValue", jsonObject.getString("totalCreditValue"));
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取e计划系列下可转让债权列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}

		return rtnMap;
	}

	/**
	 * 获取e计划系列下已转让债权列表
	 * @param 
	 */
	@Override
	public Map<String, Object> getAssignCompList(Map<String, Object> map) throws Exception {
		Map<String, Object> rtnMap = new HashMap<String, Object>();

		String transCode = "E330009";

		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){
				List<Map<String, Object>> list = JsonUtil.toList(jsonArr);
				for (int i = 0; i < list.size(); i++) {
					Map<String, Object> tmpMap = list.get(i);
					tmpMap.put("creditState", "已转让");     // 设置交易状态
					
					list.set(i, tmpMap);
				}
				
				rtnMap.put("list", list);
				rtnMap.put("listSize", list.size());
				
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			
			rtnMap.put("totalCreditValue", jsonObject.getString("totalCreditValue"));
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取e计划系列下可转让债权列表 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", Consts.ErrorMsg.MSG00001);
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}

		return rtnMap;
	}

	@Override
	public String getEPlanProductType() {
		StringBuffer productType = new StringBuffer();

		List<Finance> financeList = financeDao.getFinanceListByQueryFlag("1");
		// 设置产品类型
		if (financeList == null || financeList.size() == 0) {
			productType.append("");
		} else {
			for (int i = 0; i < financeList.size(); i++) {
				Finance finance = financeList.get(i);
				productType.append(finance.getId());
				
				if (i != financeList.size() - 1) {
					productType.append(", ");
				}
			}
		}
		
		return productType.toString();
	}
	
	/**
	 * 获取鼓钱包所有产品类型
	 * @throws Exception 
	 */
	@Override
	public String getAllProductType() throws Exception {
		StringBuffer productType = new StringBuffer();

		//List<Finance> financeList = financeDao.getFinanceListByQueryFlag("1");
		List<Finance> financeList = financePlanOpfDao.getFinanceList();
		// 设置产品类型
		if (financeList == null || financeList.size() == 0) {
			productType.append("");
		} else {
			for (int i = 0; i < financeList.size(); i++) {
				Finance finance = financeList.get(i);
				productType.append(finance.getId());
				
				if (i != financeList.size() - 1) {
					productType.append(", ");
				}
			}
		}
		
		return productType.toString();
	}

	/**
	 * 转让退出获取订单列表(可转让，转让中，已转让)(2015/6/18新增)
	 */
	@Override
	public Map<String, Object> getOrderListByType(Map<String, Object> map)
			throws Exception {
		
		Map<String, Object> rtnMap = new HashMap<String, Object>();

		String transCode = "E330023";

		// 构建json字符串
		String jsonStr = CommonUtil.constructJsonStr(transCode, (String) map.get("userId"), map);
		
		// 调用核心接口
		String rtnStr = CommonUtil.getCoreValue(jsonStr);
		JSONObject jsonObject = JSONObject.fromObject(rtnStr);
		// rescode = "00000"
		if(Consts.SUCCESS_CODE.equals(jsonObject.getString("rescode"))){
			JSONArray jsonArr = jsonObject.getJSONArray("list");
			if(jsonArr != null && jsonArr.size() > 0){			
				rtnMap.put("list", jsonArr);
				rtnMap.put("listSize", jsonArr.size());
			} else {
				rtnMap.put("list", new ArrayList<Object>());
				rtnMap.put("listSize", "0");
			}
			
			rtnMap.put("totalNum", jsonObject.getString("totalNum"));
			
			// 设置返回code
			rtnMap.put("rescode", Consts.SUCCESS_CODE);
			rtnMap.put("resmsg", Consts.SUCCESS_DESCRIBE);
			rtnMap.put("resmsg_cn", "");
		} else {
			logger.debug("=====获取转让退出订单列表（可转让，转让中，已转让） 核心接口调用异常=====");
			logger.debug("核心接口：" + transCode);
			logger.debug("rescode：" + jsonObject.getString("rescode") + "；  resmsg：" + jsonObject.getString("resmsg"));
			rtnMap.put("rescode", Consts.ERROR_CODE);
			rtnMap.put("resmsg", "");
			rtnMap.put("resmsg_cn", Message.CnDescriptionExpression.BUSINESSE_ERROR);
		}

		return rtnMap;
	}
	
}
